package com.kitty.game;

import com.kitty.game.config.Server;
import com.kitty.mina.cache.DataCache;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

@Service
public class ServerService {
    int port;
    public static int neice;
    String inetIp;
    int merge;
    public static int serverId;
    /**登录服url*/
    @Getter
    private String loginServerUrl;

    public static int gate;

    public ConcurrentHashMap<Long,Long> moduleId_count = new ConcurrentHashMap<>();


    ScheduledExecutorService scheduledExecutorService;

    public void init(){
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(12);
        setScheduledExecutorService(scheduledExecutorService);
        neice = Integer.parseInt(DataCache.REDIS_DATA.get("neice").toString());
        inetIp = DataCache.REDIS_DATA.get("inetIp").toString();
        loginServerUrl = DataCache.REDIS_DATA.get("login_url").toString();
        port = Integer.parseInt(DataCache.REDIS_DATA.get("port").toString());
        merge = Integer.parseInt(DataCache.REDIS_DATA.get("merge").toString());
        gate = Integer.parseInt(DataCache.REDIS_DATA.get("gate").toString());
        serverId = Integer.parseInt(DataCache.REDIS_DATA.get("serverId").toString());

    }

    public int getMerge() {
        return merge;
    }

    public void setMerge(int merge) {
        this.merge = merge;
    }

    public ConcurrentHashMap<Long, Long> getModuleId_count() {
        return moduleId_count;
    }

    public void setModuleId_count(ConcurrentHashMap<Long, Long> moduleId_count) {
        this.moduleId_count = moduleId_count;
    }

    public ScheduledExecutorService getScheduledExecutorService() {
        return scheduledExecutorService;
    }

    public void setScheduledExecutorService(ScheduledExecutorService scheduledExecutorService) {
        this.scheduledExecutorService = scheduledExecutorService;
    }



    public int getNeice() {
        return neice;
    }


    public void setNeice(int neice) {
        this.neice = neice;
    }



    public int getPort() {
        return port;
    }

    public String getInetIp() {
        return inetIp;
    }

//    public Server getServer(String ip, int port) {
//        for (Map.Entry<Integer, Server> entry :DataCache.ID_SERVER.entrySet()) {
//            if (entry.getValue().getSonIp().equals(ip) && entry.getValue().getSonPort()==port){
//                return entry.getValue();
//            }
//        }
//        return null;
//    }

    public Server getKuafuServer(){
        for (Map.Entry<Integer, Server> entry :DataCache.ID_SERVER.entrySet()) {
            if (entry.getValue().getId() == 10000){
                return entry.getValue();
            }
        }
        return null;
    }

    public Server getServer() {
        for (Map.Entry<Integer, Server> entry :DataCache.ID_SERVER.entrySet()) {
            if (entry.getValue().getIp().equals(inetIp) && entry.getValue().getPort()==port){
                return entry.getValue();
            }
        }
        return null;
    }


    public boolean isLocal() {
        if (getServer().getSonIp().startsWith("192.168.1.101")) {
            return true;
        }
        return false;
    }

}
